package com.geen.tuoke.ui.activity.phone

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import com.chad.library.adapter.base.module.LoadMoreModule
import com.geen.tuoke.R
import com.geen.tuoke.base.BaseActivity
import com.geen.tuoke.base.BaseAdapter
import com.geen.tuoke.base.EmptyViewModel
import com.geen.tuoke.bean.CustomerInfo
import com.geen.tuoke.bean.SearchTaskInfo
import com.geen.tuoke.bean.TaskTag
import com.geen.tuoke.databinding.ActivityTaskSearchBinding
import com.geen.tuoke.databinding.ItemCustomListBinding
import com.geen.tuoke.databinding.ItemTaskSearchBinding
import com.geen.tuoke.databinding.ItemTaskSearchTagBinding
import com.geen.tuoke.ui.activity.AddOrEditCustomerActivity
import com.geen.tuoke.ui.viewmodel.SearchTaskVm
import com.geen.tuoke.utils.IntentConstants
import com.geen.tuoke.utils.ext.initClose
import com.geen.tuoke.widget.CustomLoadMoreView
import com.geen.tuoke.widget.EmptyLayout
import com.seeway.wms.utils.startActivity
import kotlinx.android.synthetic.main.include_toolbar.*

/**
 *
 * @Description: 搜索任务
 * @Author: geen
 * @CreateDate: 2022/1/3 16:42
 * @Version: 1.0
 */
class SearchTaskActivity : BaseActivity<SearchTaskVm, ActivityTaskSearchBinding>(),
    SearchTaskVm.SearchTaskHandler {

    private lateinit var searchTaskAdapter: BaseAdapter<SearchTaskInfo, ItemTaskSearchBinding>
    private lateinit var searchCustomerAdapter : BaseAdapter<CustomerInfo, ItemCustomListBinding>

    override fun getLayoutId(): Int {
        return R.layout.activity_task_search
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        with(mBinding) {
            lifecycleOwner = this@SearchTaskActivity
            viewModel = mViewModel
            handler = this@SearchTaskActivity
        }
        initView()

        mViewModel.dataPage.observe(this, {
            if (it == null) {
                return@observe
            }
            //如果有下拉刷新 处理
            if (mViewModel.pageNo == 1) {
                searchTaskAdapter.setList(it.records)
            } else {
                searchTaskAdapter.addData(it.records)
            }
            if (mViewModel.pageNo == it.totalPage) {
                searchTaskAdapter.loadMoreModule.loadMoreEnd()
            } else {
                searchTaskAdapter.loadMoreModule.loadMoreComplete()
            }
            mViewModel.pageNo++
        })

        mViewModel.customInfo.observe(this,{
            if(it!=null){
                AddOrEditCustomerActivity.start(this, it)
            }
        })
    }

    private fun initView() {
        mBinding.viewBack.setOnClickListener {
            finish()
        }

        mBinding.etSearch.setOnEditorActionListener { v, actionId, event ->
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    //调用接口
                    mViewModel.taskQuery()
                }
            }
            false
        }

        searchTaskAdapter =
            object : BaseAdapter<SearchTaskInfo, ItemTaskSearchBinding>(R.layout.item_task_search),
                LoadMoreModule {
                init {
                    addChildClickViewIds(R.id.tvAdd)
                }

                override fun onUpdate(
                    binding: ItemTaskSearchBinding,
                    item: SearchTaskInfo,
                    position: Int
                ) {
                    binding.run {
                        searchTask = item
                    }
                }
            }
        mBinding.mRecycleView.adapter = searchTaskAdapter
        searchTaskAdapter.setEmptyView(EmptyLayout(this))
        searchTaskAdapter.loadMoreModule.loadMoreView = CustomLoadMoreView()
        searchTaskAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = true
        searchTaskAdapter.loadMoreModule.isEnableLoadMore = true
        searchTaskAdapter.loadMoreModule.isAutoLoadMore = true

        searchTaskAdapter.loadMoreModule.setOnLoadMoreListener {
            mViewModel.pageNo=1
            mViewModel.taskQuery()
        }

        searchTaskAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.tvAdd -> {
                    val searchItem = searchTaskAdapter.getItem(position)
                    if(searchItem.isClient==1){
                        mViewModel.getCustomInfoById(searchItem.clientId)
                    }else{
                        AddOrEditCustomerActivity.start(this, null)
                    }
                }
            }
        }

        //tag
        val tagList = arrayListOf<TaskTag>()
        tagList.add(TaskTag("公司", true, 2))
        tagList.add(TaskTag("电话", false, 0))
        tagList.add(TaskTag("联系人", false, 1))
        tagList.add(TaskTag("备注", false, 3))
        val tagAdapter = object : BaseAdapter<TaskTag, ItemTaskSearchTagBinding>(
            R.layout.item_task_search_tag,
            tagList
        ) {
            override fun onUpdate(binding: ItemTaskSearchTagBinding, item: TaskTag, position: Int) {
                binding.run {
                    tvName.isSelected = item.isSelect
                    tvName.text = tagList.get(position).tagName
                }
            }
        }

        tagAdapter.setOnItemClickListener { adapter, view, position ->
            tagAdapter.data.forEach {
                it.isSelect = false
            }
            mViewModel.currentTaskTag.value = tagAdapter.getItem(position)
            tagAdapter.getItem(position).isSelect = true
            mViewModel.pageNo=1
            tagAdapter.notifyDataSetChanged()
        }

        mViewModel.currentTaskTag.value = tagList[0]

        mBinding.mTagRecycleView.adapter = tagAdapter
    }

    override fun clickCancel(view: View) {
        mViewModel.queryContent.value = null
        searchTaskAdapter.setList(arrayListOf())
    }

}